package com.engine.jucailinkq.attendance.workflow.action;

import com.engine.common.util.ServiceUtil;
import com.engine.jucailinkq.attendance.workflow.service.MakeUpClockInService;
import com.engine.jucailinkq.attendance.workflow.service.impl.MakeUpClockInServiceImpl;
import com.engine.jucailinkq.common.util.CommonUtil;
import com.engine.jucailinkq.common.util.DateUtil;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 出差流程归档action
 */
@Slf4j
public class EvectionArchivingAction implements Action {


    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        // 流程表单主表数据
        HashMap<String,String> mainTableData = CommonUtil.getMainTableInfo(requestInfo);
        // 流程表单明细表数据
        List<Map<String, String>> detailTableData = CommonUtil.getDetailTableInfo(requestInfo,0);
        //出差人员
        String ccr = mainTableData.get("ccr");
        try {
            //记录需要更改出勤结果数据状态的人员、日期信息
            List<Map<String, String>> empDateList = new ArrayList<>();
            Map<String, String> empDateItem;
            List<String> dateList = new ArrayList<>();
            //遍历明细表1
            for (Map<String, String> data : detailTableData) {
                dateList = DateUtil.getDatesBetween(data.get("ksrq"), "".equals(Util.null2String(data.get("jsrq"))) ? data.get("ksrq") : data.get("jsrq"));
                for (String date : dateList) {
                    //收集需要更改出勤结果数据状态的人员、日期信息
                    empDateItem = new HashMap<>();
                    empDateItem.put("empId", data.getOrDefault("ccr", ccr));
                    empDateItem.put("cqDate", date);
                    empDateList.add(empDateItem);
                }
            }
            //更新出勤结果中的数据状态
            String sjztUpdateResult = CommonUtil.updateAttendanceResultInfoStatus(empDateList, "0");
            if (!"".equals(sjztUpdateResult)) {
                log.error("更新出勤结果中的数据状态失败:" + sjztUpdateResult);
                requestInfo.getRequestManager().setMessageid("11111" + requestInfo.getRequestid() + "22222");
                requestInfo.getRequestManager().setMessagecontent("更新出勤结果中的数据状态失败:" + sjztUpdateResult);
                return Action.FAILURE_AND_CONTINUE;
            }

        }catch (Exception e){
            log.error("EvectionArchivingAction error : [{}]",e);
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }
}
